{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "540ff471-dcea-4b3e-9c0c-a3173f1c640e",
   "metadata": {},
   "source": [
    "# Refine"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "158b08a8-32d3-4397-ad37-75870416226b",
   "metadata": {},
   "source": [
    "## Load Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "bbbac556-bb22-47e2-b8bf-80818d241858",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from llama_index import SimpleDirectoryReader"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "bc0d4087-1ee3-4c38-94c0-b34f87ea8aca",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "reader = SimpleDirectoryReader(\n",
    "    input_files=[\"../data/paul_graham/paul_graham_essay.txt\"]\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "7934bb4a-4c0f-4833-842f-7fd47e16eeae",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "docs = reader.load_data()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "bf6b6f5c-5852-41be-8ce8-d94c520e0e50",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "text = docs[0].text"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "efed56ee-fcd3-439c-a1b2-53c643f15c8e",
   "metadata": {},
   "source": [
    "## Summarize"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "4ae4dad4-6044-4c9c-becd-4e2908b54a30",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from llama_index.llms import OpenAI\n",
    "\n",
    "llm = OpenAI(model=\"gpt-3.5-turbo\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e65c577c-215e-40e9-8f3f-c23a09af7574",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from llama_index import ServiceContext\n",
    "\n",
    "service_context = ServiceContext.from_defaults(llm=llm)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "52c48278-f5b2-47bb-a240-6b66a191c6db",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from llama_index.response_synthesizers import Refine\n",
    "\n",
    "summarizer = Refine(service_context=service_context, verbose=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "834ac725-54ce-4243-bc09-4a50e2590b28",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "> Refine context: making fakes for a local antique dealer. She'd ...\n",
      "> Refine context: look legit, and the key to looking legit is hig...\n",
      "> Refine context: me 8 years to realize it. Even then it took me ...\n",
      "> Refine context: was one thing rarer than Rtm offering advice, i...\n"
     ]
    }
   ],
   "source": [
    "response = summarizer.get_response(\"who is Paul Graham?\", [text])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "a600aa73-74b8-4a20-8f56-1b273417f788",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Paul Graham is an individual who has played a crucial role in shaping the internet infrastructure and has also pursued a career as a writer. At one point, he received advice from a friend that urged him not to let Y Combinator be his final noteworthy achievement. This advice prompted him to reflect on his future with Y Combinator and ultimately led him to pass on the responsibility to others. He approached Jessica and Sam Altman to assume leadership positions in Y Combinator, aiming to secure its continued success.\n"
     ]
    }
   ],
   "source": [
    "print(response)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
